// pages/sort/sort.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // navLeft:["宝宝奶粉", "纸尿裤","辅食营养","母婴专区","护肤美体","营养保健","居家日用","进口美食","轻奢女装","秒杀特惠清仓","时尚箱包","专场大促","汽车用品","美容护肤","电器手机","生活家居","服饰鞋子","还有什么","没有了吧",]
    ulTop: 0,
    cateItems: [
      {
        cate_id: 1,
        cate_name: "全部商品",
        children: [{
          child_id: 1,
          name: "电器",
          // img: "../../images/kksk.png"
        }, {
          child_id: 2,
          name: "数码",
          // img: "../../images/kksk.png"
        }, {
          child_id: 3,
          name: "衣饰",
          // img: "../../images/kksk.png"
        }, {
          child_id: 4,
          name: "日用品",
          // img: "../../images/kksk.png"
        }, {
          child_id: 5,
          name: "化妆品",
          // img: "../../images/kksk.png"
        }]
      },
      {
        cate_id: 1,
        cate_name: "新品",
        children: []
      },
      {
        cate_id: 2,
        cate_name: "百货",
        children: [{
            type_id: 3,
            child_id: 1,
            name: "全部",
            // img: "../../images/kksk.png"
          }, {
            type_id: 3,
            child_id: 2,
            name: "纸品湿巾",
            // img: "../../images/kksk.png"
          }, {
            type_id: 3,
            child_id: 3,
            name: "清洁用具",
            // img: "../../images/kksk.png"
          }, {
            type_id: 3,
            child_id: 4,
            name: "日用百货",
            // img: "../../images/kksk.png"
          },
          {
            type_id: 3,
            child_id: 5,
            name: "收纳洗晒",
            // img: "../../images/kksk.png"
          },
          {
            type_id: 3,
            child_id: 6,
            name: "珠宝饰品",
            // img: "../../images/kksk.png"
          },
          {
            type_id: 3,
            child_id: 7,
            name: "洗衣清洁",
            // img: "../../images/kksk.png"
          },
        ]
      },
      {
        cate_id: 3,
        cate_name: "智能先锋",
        children: [{
          type_id: 3,
          child_id: 1,
          name: "洗衣清洁",
          // img: "../../images/kksk.png"
        }, ],
      }
    ],

    searchVal: '智能自动洗菜机',
    curNav: 1,
    /* 此变量用于判断该显示某个子item */
    curIndex: 1,
    childId: 1,
    sortItem: [{
      title: '销量',
      isImg: 0
    }, {
      title: '新款',
      isImg: 0
    }, {
      title: '价格',
      isImg: 1

    }],
    sortIndex: 0,
    sortSrc: "",
    sortX: "",
    sortDown: "",
    sortUp: "",
    goodsItem: [{
      goodsId: 684,
      date: 138487,
    }, {
      goodsId: 588,
      date: 138487,
    }, {
      goodsId: 659,
      date: 18688,
    }, {
      goodsId: 38,
      date: 546464684,
    }, {
      goodsId: 158,
      date: 1868835153,
    }, {
      goodsId: 287,
      date: 368777,
    }, {
      goodsId: 987,
      date: 3655358,
    }, {
      goodsId: 255,
      date: 3225892,
    }, {
      goodsId: 723,
      date: 6814889
    }, {
      goodsId: 28,
      date: 6165486846,
    }, {
      goodsId: 587,
      date: 16533848,
    }, {
      goodsId: 357,
      date: 3516844422,
    }, {
      goodsId: 254,
      date: 575336877,
    }, {
      goodsId: 785,
      date: 333222,
    }, {
      goodsId: 578,
      date: 2647872,
    }, {
      goodsId: 687,
      date: 59569982,
    }, {
      goodsId: 389,
      date: 6202677,
    }, {
      goodsId: 677,
      date: 3354217,
    }, {
      goodsId: 387,
      date: 1136358
    }, {
      goodsId: 248,
      date: 35782332,
    }, {
      goodsId: 347,
      date: 26566333,
    }, {
      goodsId: 777,
      date: 5696265,
    }, {
      goodsId: 357,
      date: 3556888,
    }, {
      goodsId: 396,
      date: 8742492,
    }],
    heightHtml: 0,
    heightA: 0,
    heightB: 0,
    heightScroll: 0,
    getGoodsList: {}, //存放接口获取到的数据
    leftItem: [], //存放左侧栏数据 
    rightItem: [], //存放右侧栏数据 
  },
  /* 把点击到的某一项 设为当前curNav   */
  switchRightTab: function(e) {
    let id = e.currentTarget.dataset.id;
    let that = this

    if (id != this.data.curNav) { //判断切换是否才触发（子盒子当前选中归零）
      this.setData({
        childId: 1
      })
    }
    this.setData({
      curNav: id,
      curIndex: id,

    })
    console.log(e.currentTarget.dataset.id, this.data.curNav, this.data.curIndex);


    wx.request({ //商品分类接口
      url: 'http://192.168.0.101/goods/type',
      method: 'GET',
      data: {
        themeId: that.data.curNav
      },
      dataType: 'json',
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        console.log(res)
        that.setData({
          rightItem: res.data
        })
        console.log(that.data.rightItem.data)
      }
    })


  },
  serItem: function(e) {

    let chiId = e.currentTarget.dataset.id
    this.setData({
      childId: chiId,
    })
    // console.log(chiId)
  },
  sortThis: function(e) {
    if (this.data.sortIndex == 2) {



    } else {
      this.setData({
        sortSrc: this.data.sortX
      })
    }

    if (this.data.sortIndex != e.currentTarget.dataset.id) {
      //判断下标是否变化触发事件
      // console.log(e.currentTarget.dataset.id)
      this.setData({
        sortIndex: e.currentTarget.dataset.id
      })

      if (this.data.sortIndex == 2) { //下标变化且等于2
        // console.log(this.data.sortIndex)
        this.setData({
          sortSrc: this.data.sortUp
        })
      } else { //不等于2
        this.setData({
          sortSrc: this.data.sortX
        })
      }

    } else if (this.data.sortIndex == 2) { //下标不变且等于2
      // console.log('up,down')

      if (this.data.sortSrc == this.data.sortUp) { //箭头为上
        this.setData({
          sortSrc: this.data.sortDown
        })
      } else { //箭头为下
        this.setData({
          sortSrc: this.data.sortUp
        })
      }
    }

  },

  toDetail: function(e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../goodsDetails/goodsDetails?id=' + e.currentTarget.dataset.id,
    })
  },
  testImg(e) { //测试图片懒加载
    // console.log(e)
  },
  ulScroll(e) { // 页面滚动时执行
    console.log(e)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    // let syncNum = 0
    wx.createSelectorQuery().select('.goodsLine').boundingClientRect(function(e) {
      // console.log(e.height)
      // that.setData({
      //   scrollHeight: e.height+'px'
      // })
      // console.log(that.data.scrollHeight)
    }).exec()


    wx.request({ //热品推荐接口
      url: 'http://192.168.0.101/goods/hot_product',
      method: 'GET',
      data: {},
      dataType: 'json',
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        console.log(res)
        that.setData({
          getGoodsList: res.data.list
        })
      }
    })
    wx.request({ //商品主题接口
      url: 'http://192.168.0.101/goods/theme',
      method: 'GET',
      data: {},
      dataType: 'json',
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        console.log(res)
        that.setData({
          leftItem: res.data
        })


        wx.request({ //商品分类接口
          url: 'http://192.168.0.101/goods/type',
          method: 'GET',
          data: {
            themeId: 1
          },
          dataType: 'json',
          header: {
            'content-type': 'application/json',
          },
          success(res) {
            console.log(res)
            // syncNum = 0
            that.setData({
              rightItem: res.data
            })
          }
        })

      }
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})